<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class fournisseur_ctr extends CI_Controller {  

	    function __construct()
    {
        parent::__construct();
        
		$this->load->library('grocery_CRUD');  
		$this->load->helper(array('form', 'url'));
		$this->load->library('image_lib');
		$this->load->helper("file");
        $this->load->helper('form'); 
 
    }
	  
	 function index()
	{
		//load articles and chapitres
		$this->load->model('charticle_mdl'); // load the file welcome_mdl.php
		$r1['charticle'] = $this->charticle_mdl->get_all(); // call the method filtera inside the class welcome_mdl
		
		$this->load->model('fournisseur_mdl'); // load the file welcome_mdl.php
		$r2['fournisseur'] = $this->fournisseur_mdl->get_all(); // call the method getall inside the class welcome_mdl
		
		$r['charticle'] = $r1 ;
		$r['fournisseur'] = $r2 ; // 
		
		//$this->load->view('fournisseur_vew',$r);  // send the result from the model to the view 
		$this->load->view('menu_right_vew',$r);  // send the result from the model to the view 
		
		
		
		
	}
	function aa(){
		$this->load->view('go');
		}
	
		public function load($str_vew)
	{
		$session = $this->session;
		$login_state =$this->session->userdata('login_state');
		if (!$login_state) 
		{
		//// go to login
		$this->load->view('login_vew');
		}
		else
		{
		$this->load->view($str_vew);
		}
		
	}
	
	public function add (){
		
		$this->load->model('fournisseur_mdl');
		$this->load->model('activite_mdl');
		$data1['rec1'] = $this->fournisseur_mdl->get_all();
		$data2['rec2'] = $this->activite_mdl->get_all();
		$r['liste_fourn'] = $data1;
		$r['liste_activite'] = $data2;
		
		$this->load->view('fiche_fourn_vew',$r); 
		
	}
	
		public function detail ($fournisseur_id){
		
		$this->load->model('fournisseur_mdl');
		$this->load->model('activite_mdl');
		$this->load->model('banque_mdl');
		
		
		$data1['rec'] = $this->fournisseur_mdl->get_fournisseur($fournisseur_id); // get  one fournisseur per id
		$r_activity_id   = $this->activite_mdl->get_activity_per_fournisseur($fournisseur_id); // get one activity per fournisseur
		$activity_id     = $r_activity_id[0]->id_activite;
		$data4['rec'] = $this->activite_mdl->get_activity_per_id($activity_id);  //get one activity for the fournisseur  
		$data2['rec'] = $this->fournisseur_mdl->get_all();   // get all  for autocomplete
		$data3['rec'] = $this->activite_mdl->get_all();  //get all activity list for autocomplete 
		$data5['rec'] = $this->banque_mdl->get_bank_per_fournisseur($fournisseur_id); // get the bank details for the fournisseur
		
		$r['fournisseur']    = $data1;
		$r['liste_fourn']    = $data2;
		$r['liste_activite'] = $data3;
		$r['activite']       = $data4;
		$r['liste_bank']     = $data5;
		
		$this->load->view('fournisseur_details_vew',$r); 
	}
		public function find_name()
    {
		
		$v=$this->input->post('nom');
		$k=0;
		}
	public function update ()
	{
		$errors = array();
		// check for data concistency
		$phone= $this->input->post('phone');
		$mobile= $this->input->post('mobile');
		$rc= $this->input->post('rc');
		$rib= $this->input->post('rib');
		$email= $this->input->post('email'); 
				
		
		if (!(preg_match('/\\(0\\d{2}\\)-\\d{2}-\\d{2}-\\d{2}/', $phone)))
		{
			 $errors['phone'] = TRUE;
		}
		if (!(preg_match('/\\(0(5|6|7)\\d{2}\\)-\\d{2}-\\d{2}-\\d{2}/', $mobile)))
		{
			 $errors['mobile'] = TRUE;
		}
		if (!(preg_match('/\\d{2}\\\/\\d{2}-\\d{5}(\\d){0,4}[a-z,A-Z]\\d{2}/', $rc)))
		{
			 $errors['rc'] = TRUE;
		}
		if (!(preg_match('/\\d{3}-\\d{5}-\\d{10}-\\d{2}/', $rib)))
		{
			 $errors['rib'] = TRUE;
		}
		if($email != FALSE) // if email was typed check it's syntax
		{
			if (!(preg_match('/[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}/', $email)))
			{
				 $errors['email'] = TRUE;
			}
		}
		
		/// if no errors do update
		if (empty($errors)) { 
			$m = 1;
		}
		else{
			// do not update
			$mm = 1;
		}
		 
	}
	
		public function get_fournisseurs_per_article()
    {
		
	$article_id = $_POST['record_id']; //set the record ID
    $this->load->model('my_custom_model');   //load the model file
	$this->my_custom_model->list_fourn_per_article($article_id); // create a query temporary table so crud can access and render it
    
	$crud = new grocery_CRUD();
		
	//	 $crud->set_theme('datatables');
    $crud->set_table('my_temp_table');
	$crud->set_primary_key('id_article', 'my_temp_table');
     
	$crud->unset_delete();
	$crud->unset_add();
	$crud->unset_edit();
	$crud->unset_read();
	 
	//$crud->add_action('Smileys', 'http://www.grocerycrud.com/assets/uploads/general/smiley.png', 'welcome/index');
	
	 
    //$crud->set_subject('conventions');
    // $crud->set_relation('id_chapitre','chapitres','nom_chapitre');
    // $crud->required_fields('id_convention_fk');
    // $crud->display_as('id_chapitre','nom');
    // $crud->fields('id_article','nom','solde','id_chapitres');
    $crud->columns('id_article','nom','solde','nom_chapitre');
	
       // $crud->set_table('articles');
        $output = $crud->render();
 
        $this->load->view('list_fourn_per_article_vew',$output);
		$this->my_custom_model->clr_list_fourn_per_article();  //drop the "temporary table"
                 
    }
}
